Imports System
Imports System.Xml

Public Class StrikesMsg
	Inherits BaseMsg

	Protected m_startterm As String
	Protected m_endterm As String
	Protected m_strikes As New Collection

	'<CPCAPI v="1.1">
	'      <StrikeReq ReqID="testID" ProductID="LN" StartTerm="200706" EndTerm="200712"/>
	'</CPCAPI>

	'<CPCAPI v="1.1">
	'      <StrikeRsp ReqID="testID" ProductID="LN" StartTerm="200706" EndTerm="200712">
	'            <Strike P="1.750"/>
	'            <Strike P="2.000"/>
	'            <Strike P="2.250"/>

#Region "Properties"

	Public Property StartTerm() As String
		Get
			Return m_startterm
		End Get
		Set(ByVal Value As String)
			m_startterm = Value
		End Set
	End Property

	Public Property EndTerm() As String
		Get
			Return m_endterm
		End Get
		Set(ByVal Value As String)
			m_endterm = Value
		End Set
	End Property

#End Region

	Public Sub New()
		RequestType = "StrikeReq"
		ResponseType = "StrikeRsp"
	End Sub

	Public Function ValidateStrike(ByVal strikep As String) As Boolean

        If m_strikes.Count = 0 Then
            GetStrikes()
        End If

        If Trim(strikep) = "" Then strikep = "0"

        Dim strike As Single
        Dim sstrikep As Single = Convert.ToSingle(strikep)

        For Each strike In m_strikes
            If strike = sstrikep Then
                Return True
            End If
        Next

        Return False
    End Function

    Private Sub GetStrikes()
        Dim dbi As dbInterface = Globals.dbi
        Dim sql As String

        ' need to NOT go to the local database to get the strike prices, as they may be updated throughout the day.
        'sql = "select * from nymx_prod_strikes where prod_id=" & dbi.quote(m_prodid)
        'sql &= " and first_mmy=" & dbi.quote(StartTerm)
        'If EndTerm = "" Then
        '    sql &= " and last_mmy=" & dbi.quote(StartTerm)
        'Else
        '    sql &= " and last_mmy=" & dbi.quote(EndTerm)
        'End If

        'dbi.query(sql)

        'While (dbi.eof() = False)
        '    m_strikes.Add(Convert.ToSingle(dbi.getval("strike_price")))
        '    dbi.movenext()
        'End While

        If m_strikes.Count = 0 Then
            Me.Submit()
        End If
    End Sub

    Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

        If m_prodid = "" Then
            Return False
        End If

        If m_endterm = "" Then m_endterm = m_startterm

        XmlW.WriteAttributeString("ProductID", m_prodid)
        XmlW.WriteAttributeString("StartTerm", m_startterm)
        XmlW.WriteAttributeString("EndTerm", m_endterm)

        Return True
    End Function

    Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

        Dim prod As XmlNode
        Dim attr As XmlAttribute

        Dim dbi As dbInterface = Globals.dbi

        If node.Attributes("ProductID").InnerText = m_prodid Then
            Dim sql As String

            sql = "delete from nymx_prod_strikes where prod_id=" & dbi.quote(m_prodid)
            sql &= " and first_mmy=" & dbi.quote(StartTerm)
            sql &= " and last_mmy=" & dbi.quote(EndTerm)

            dbi.nonquery(sql)

            For Each prod In node.ChildNodes
                InsertStrike(prod)
            Next
        End If

    End Sub

    Private Sub InsertStrike(ByRef prod As XmlNode)
        '<Strike P="1.750"/>
        Dim dbi As dbInterface = Globals.dbi
        Dim term As String

        If prod.Name = "Strike" Then
            term = prod.Attributes("P").InnerText

            m_strikes.Add(Convert.ToSingle(term))

            dbi.SetupStoredProc("sp_nymx_ins_product_strike")
            dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, m_prodid)
            dbi.AddStoredProcParam("@first_mmy", SqlDbType.VarChar, StartTerm)
            dbi.AddStoredProcParam("@last_mmy", SqlDbType.VarChar, EndTerm)
            dbi.AddStoredProcParam("@strike_price", SqlDbType.VarChar, term)
            dbi.RunStoredProc()
        End If

    End Sub
End Class
